//
//  SYJToBarView.swift
//  weibo
//
//  Created by 孙英建 on 16/4/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class SYJToBarView: UIView {

    // 转发
    var retweetButton: UIButton?
    
    // 评论
    var commentButton: UIButton?
    
    // 赞
    var unlikeButton: UIButton?
    
    // 按钮点击回调闭包
    var buttonClickClosure: ((tag: Int) -> ())?
    
    // 存储数据
    var statusModel: SYJStatusesModel?{
        didSet{
            let retweetResult = getCountString("转发", count: statusModel?.reposts_count ?? 0)
            let commentResult = getCountString("评论", count: statusModel?.comments_count ?? 0)
            let unlikeResult = getCountString("赞", count: statusModel?.attitudes_count ?? 0)
            
            
            retweetButton?.setTitle(retweetResult, forState: UIControlState.Normal)
            commentButton?.setTitle(commentResult, forState: UIControlState.Normal)
            unlikeButton?.setTitle(unlikeResult, forState: UIControlState.Normal)
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 转发评论赞的点击事件
    @objc private func clickButton(sender: UIButton){
        buttonClickClosure!(tag: sender.tag)
    }
    
    
    // 设置转发评论赞
    func getCountString(title: String, count: Int) -> String{
        
        if count <= 0 {
            return title
        }else if count > 0 && count < 10000 {
            return "\(count)"
        }else{
            let num = CGFloat(count / 1000) / 10
            var countString = "\(num)万"
            // 判断是否是整数，去掉后边的.0
            if countString.containsString(".0"){
                countString = countString.stringByReplacingOccurrencesOfString(".0万", withString: "万")
            }
            return countString
        }
    }

    
    // 添加设置控件
    func setUI(){
        
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        // 按钮
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发", tag: 3001)
        commentButton =  addChildButton("timeline_icon_comment", title: "评论", tag: 3002)
        unlikeButton =  addChildButton("timeline_icon_unlike", title: "赞", tag: 3003)
        
        // 两根竖线
        let line1 = addChildLineImageView()
        let line2 = addChildLineImageView()
        
        
        // 添加约束
        // 三个按钮
        retweetButton!.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        commentButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
        }
        
        unlikeButton!.snp_makeConstraints { (make) -> Void in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        // 两条竖线
        line1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(retweetButton!)
        }
        
     
        line2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(commentButton!)
        }
    }
    
    
    // MARK: - 添加控件的方法
    // 按钮
    func addChildButton(imgName: String, title: String, tag: Int) -> UIButton{
        
        let btn = UIButton()
        
        btn.tag = tag
        
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(btn)
        
        // 点击事件
        btn.addTarget(self, action: "clickButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }
    
    // 创建竖线
    func addChildLineImageView() -> UIImageView{
        let img = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(img)
        return img
    }
}













